﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace Expect.Core.Test
{
    class Helper
    {
        /// <summary>
        /// Load a ressource file into a string
        /// </summary>
        /// <param name="ressourceFileName"></param>
        /// <returns></returns>
        public static string LoadFromRessourceFile(string ressourceFileName)
        {
            string res = null;
            UseRessourceStream(
                ressourceFileName,
                input =>
                {
                    using (StreamReader reader = new StreamReader(input))
                    {
                        res = reader.ReadToEnd();
                    }
                }
            );
            return res;
        }

        public static void UseRessourceStream(string ressourceFileName, Action<Stream> actInput)
        {
            Assembly ass = typeof(Helper).Assembly;
            string[] ressources = ass.GetManifestResourceNames();
            foreach (string resname in ressources)
            {
                if (resname.EndsWith(ressourceFileName))
                {
                    using (Stream input = ass.GetManifestResourceStream(resname))
                    {
                        actInput(input);
                    }
                }
            }
        }
    }
}
